using System;
using System.ComponentModel;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using AttributeCollection = System.Web.UI.AttributeCollection;
using System.Web.UI.HtmlControls;

namespace Library.WebControls.SimpleTextEdit {
	[ToolboxBitmap(typeof(SpTextEdit), "Library.WebControls.SimpleTextEdit.SpTextEdit.bmp")]
	[Designer(typeof(SpTextEditDesign))]
	[SupportsEventValidation]
	[ToolboxData("<{0}:SpTextEdit runat=server></{0}:SpTextEdit>")]
	[DesignTimeVisible(true)]
	public class SpTextEdit : BaseControl {
		private TextBox _textBox;
		private HtmlGenericControl _divContain;

		public SpTextEdit() {
			CssFileName = BaseCssDir + "sptextedit-ui.css";
			ClientScript = new ClientScript();
		}
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		[DefaultValue("")]
		public ClientScript ClientScript { private set; get; }
		public override string ID {
			get {
				EnsureChildControls();
				return _textBox.ID;
			}
			set {
				EnsureChildControls();
				_textBox.ID = value;
			}
		}

		public override string ClientID {
			get {
				EnsureChildControls();
				return _textBox.ClientID;
			}
		}
		[DefaultValue(TextBoxMode.SingleLine)]
		public TextBoxMode TextMode {
			get {
				EnsureChildControls();
				return _textBox.TextMode;
			}
			set {
				EnsureChildControls();
				_textBox.TextMode = value;
				if (value == TextBoxMode.MultiLine) {
					_divContain.Attributes["class"] = "spMemoEdit";
				} else {
					_divContain.Attributes["class"] = "spTextEdit";
				}
			}
		}
		[DefaultValue("")]
		public virtual Unit Width {
			get {
				EnsureChildControls();

				return new Unit(_divContain.Style[HtmlTextWriterStyle.Width]);
			}
			set {
				EnsureChildControls();
				_divContain.Style[HtmlTextWriterStyle.Width] = value.ToString();
			}
		}

		//public override bool Visible {
		//  get {
		//    EnsureChildControls();
		//    return textBox.Visible;

		//  }
		//  set {
		//    EnsureChildControls();
		//    textBox.Visible = value;
		//  }
		//}
		[Browsable(false)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public AttributeCollection Attributes {
			get {
				EnsureChildControls();
				return _textBox.Attributes;
			}
		}
		[DefaultValue("")]
		public virtual string Text {
			get {
				EnsureChildControls();
				return _textBox.Text;
			}
			set {
				EnsureChildControls();
				_textBox.Text = value;
			}
		}

		[Browsable(false)]
		[DesignerSerializationVisibility((DesignerSerializationVisibility.Hidden))]
		public CssStyleCollection Style {
			get {
				EnsureChildControls();
				return _textBox.Style;
			}
		}

		[DefaultValue(true)]
		public virtual bool Wrap {
			get {
				EnsureChildControls();
				return _textBox.Wrap;
			}
			set {
				EnsureChildControls();
				_textBox.Wrap = value;
			}
		}

		[DefaultValue(false)]
		[Themeable(false)]
		[Bindable(true)]
		public virtual bool ReadOnly {
			get {
				EnsureChildControls();
				return _textBox.ReadOnly;
			}
			set {
				EnsureChildControls();
				_textBox.ReadOnly = value;
			}
		}


		[Themeable(false)]
		[DefaultValue(0)]
		public virtual int Rows {
			get {
				EnsureChildControls();
				return _textBox.Rows;
			}
			set {
				EnsureChildControls();
				_textBox.Rows = value;
			}
		}


		[Themeable(false)]
		[DefaultValue(AutoCompleteType.None)]
		public virtual AutoCompleteType AutoCompleteType {
			get {
				EnsureChildControls();
				return _textBox.AutoCompleteType;
			}
			set {
				EnsureChildControls();
				_textBox.AutoCompleteType = value;
			}
		}

		protected override void CreateChildControls() {
			_divContain = new HtmlGenericControl("div");
			_divContain.Attributes["class"] = "spTextEdit";
			var span = new HtmlGenericControl("div");
			_textBox = new TextBox();
			_textBox.BorderStyle = BorderStyle.None;
			_textBox.BorderWidth = new Unit(0);
			_textBox.Width = new Unit("100%");
			//_textBox.CssClass = "spTextEdit";
			span.Controls.Add(_textBox);
			_divContain.Controls.Add(span);
			Controls.Add(_divContain);
		}

		public override bool ClientEnable {
			get {
				EnsureChildControls();
				return !_textBox.ReadOnly;
			}
			set {
				EnsureChildControls();
				_textBox.ReadOnly = !value;
			}
		}

		public override bool ClientVisible {
			get {
				EnsureChildControls();
				return _divContain.Style[HtmlTextWriterStyle.Display] != "none";
			}
			set {
				if (value) {
					_divContain.Style.Remove(HtmlTextWriterStyle.Display);
				} else {
					_divContain.Style[HtmlTextWriterStyle.Display] = "none";
				}
			}
		}


		protected override void OnLoad(EventArgs e) {
			base.OnLoad(e);
			IncludeScriptOnLoad(typeof(SpTextEdit), "Sc", "Library.WebControls.SimpleTextEdit.include.SpTextEdit.js", UtilLib);
		}

		protected override void CreateClientObjectAtRender(HtmlTextWriter writer) {
			BeginScriptTag(writer);
			writer.Write("var {0}=new SpTextEdit('{0}','{1}');", ClientName, ClientID);
			AddScriptHandler(writer, 1, ClientScript.TextChanged, "AddHandler");

			EndScriptTag(writer);
		}

		protected override void Render(HtmlTextWriter writer) {
			_textBox.Attributes["onchange"] = ClientName + ".Call(1);";
			base.Render(writer);
		}
	}
}